<?php
/**
 * User:ayou chow
 * Date: 2019-11-26
 * Time: 10:42
 */

namespace app\admin\controller;

use app\common\model\VipPriceModel;
use think\facade\Request;
use think\Validate;

class VipController extends BaseController {
    public function index() {
        return $this->fetch();
    }

    public function add() {
        return $this->fetch();
    }

    public function edit() {
        $level   = Request::param('level');
        $vipinfo = VipPriceModel::where(['level' => $level])->find();
        $this->assign("vipinfo", $vipinfo);
        return $this->fetch();
    }

    public function getlist() {
        $vips = VipPriceModel::order("price", "asc")->all();
        if (count($vips) > 0) {
            return self::bulidSuccess($vips);
        }
        return self::bulidFail("未查询到相关数据");
    }

    public function add_post() {
        $all_param = Request::param();

        $validate = new Validate([
            'level'    => 'require|integer|egt:1',
            'price' => 'require|integer|egt:0',
            'gold' => 'require|integer|egt:0',
        ], [
            "level.require" => "请输入等级",
            "level.integer"  => "等级必须为整数",
            "level.egt"  => "等级不能小于1",
            "price.length"  => "请输入价格",
            "price.integer"  => "价格必须为整数",
            "price.egt"  => "价格不能小于0",
            "gold.require"    => "请输入钻石数量",
            "gold.integer"  => "钻石数量必须为整数",
            "gold.egt"  => "钻石数量不能小于0",
        ]);
        if (!$validate->check($all_param)) {
            return self::bulidFail($validate->getError());
        }

        $VipPriceModel = new VipPriceModel(Request::param());
        if ($VipPriceModel->save()) {
            return self::bulidSuccess();
        }
        return self::bulidFail();
    }

    public function edit_post() {
        $all_param = Request::param();

        $validate = new Validate([
            'level'    => 'require|integer|egt:1',
            'price' => 'require|integer|egt:0',
            'gold' => 'require|integer|egt:0',
        ], [
            "level.require" => "请输入等级",
            "level.integer"  => "等级必须为整数",
            "level.egt"  => "等级不能小于1",
            "price.length"  => "请输入价格",
            "price.integer"  => "价格必须为整数",
            "price.egt"  => "价格不能小于0",
            "gold.require"    => "请输入钻石数量",
            "gold.integer"  => "钻石数量必须为整数",
            "gold.egt"  => "钻石数量不能小于0",
        ]);
        if (!$validate->check($all_param)) {
            return self::bulidFail($validate->getError());
        }
        $VipPriceModel = VipPriceModel::where(['level' => Request::param('level')])->find();
        if (!$VipPriceModel) {
            return self::bulidFail("条目不存在");
        }

        if ($VipPriceModel->save(Request::param())) {
            // 记录日志
            $log_content = "编辑VIP配置，等级ID：{$VipPriceModel->level}，价格：{$VipPriceModel->price}，赠送数量：{$VipPriceModel->gold}";
            parent::addAdminLog($log_content);

            return self::bulidSuccess();
        }
        return self::bulidFail();
    }

    public function del_post() {
        $id             = Request::param('id');
        $vipprict_model = VipPriceModel::get($id);
        if (!$vipprict_model) {
            return self::bulidFail("条目不存在");
        }

        if (VipPriceModel::destroy($id)) {
            // 记录日志
            $log_content = "删除VIP配置，等级ID：{$vipprict_model->level}，价格：{$vipprict_model->price}，赠送数量：{$vipprict_model->gold}";
            parent::addAdminLog($log_content);

            return self::bulidSuccess();
        }
        return self::bulidFail();
    }
}